package com.example.myapplication.activity;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.luck.picture.lib.tools.ScreenUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: wangrongfeng
 * @create: 2023-05-11 19:46
 **/
public class DrawerFrameView extends FrameLayout {
    private int mLastY, mLastX;


    /** 记录按下的坐标点（起始点）**/
    private float mPosX = 0;
    private float mPosY = 0;
    /** 记录移动后抬起坐标点（终点）**/
    private float mCurPosX = 0;
    private float mCurPosY = 0;

    private List<View> canTouchViews = new ArrayList<>();

    //构建手势探测器


    public DrawerFrameView(@NonNull Context context) {
        super(context);
    }

    public DrawerFrameView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawerFrameView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    void init(View view_pager){
        view_pager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                System.out.println("sssss 1111 " + event.getAction());
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mPosX = event.getX();
                        mPosY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurPosX = event.getX();
                        mCurPosY = event.getY();
                        if (mCurPosY - mPosY > 0
                                && (Math.abs(mCurPosY - mPosY) > 25)) {
                            //向下滑動

                        } else if (mCurPosY - mPosY < 0
                                && (Math.abs(mCurPosY - mPosY) > 25)) {
                            //向上滑动
                        }
                        break;
                }
                return true;
            }
        });

    }



    public void addTouchViews(View... views) {
        canTouchViews.addAll(Arrays.asList(views));
    }

    public void removeAllTouchViews() {
        canTouchViews.clear();
    }

    private boolean isDisallowInterceptTouch;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println("sssss 2222 " + ev.getAction() );
//        int x = (int) ev.getX();
//        int y = (int) ev.getY();
//        if (!isDisallowInterceptTouch) {
//            for (View v : canTouchViews) {
//                if (isTouchPointInView(v, x, y)) {
//                    isDisallowInterceptTouch = true;
//                    break;
//                }
//            }
//        }
//        if (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_CANCEL) {
//            isDisallowInterceptTouch = false;
//        }
//        requestDisallowInterceptTouchEvent(isDisallowInterceptTouch);
//        System.out.println("sssssss  isDisallowInterceptTouch " + isDisallowInterceptTouch);
        return super.dispatchTouchEvent(ev);
    }


    public boolean isTouchPointInView(View view, int x, int y) {
        if (view == null) {
            return false;
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        if (y >= top && y <= bottom && x >= left && x <= right) {
            return true;
        }
        return false;
    }

    private String getOrientation(float dx, float dy) {
        if (Math.abs(dx) > Math.abs(dy)) {
            //X轴移动
            return dx > 0 ? "R" : "L";
        } else {
            //Y轴移动
            return dy > 0 ? "B" : "T";
        }
    }

}
